<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in employees"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select></el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// 导入ajax
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, deDepartDetail } from '@/api/departments'
export default {
  props: {
    curId: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      //   验证规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      //   接收人员数据
      employees: []
    }
  },
  created() {
    // 人员信息
    this.loadEmployeeSimple()
    // 表单内容信息
    if (this.isEdit) {
      this.loadDepartDetail()
    }
  },
  methods: {
    // 人员信息
    async loadEmployeeSimple() {
      try {
        const res = await getEmployeeSimple()
        this.employees = res.data
        console.log(res, '新增部门')
      } catch (error) {
        console.log(error)
      }
    },
    // 新增ajax
    async loadDepartments() {
      try {
        console.log(this.curId)
        const res = await addDepartments({ pid: this.curId, ...this.form })
        // 关闭弹层和渲染
        this.$emit('success')
        console.log(res, '新增')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取编辑子部门的信息的ajax
    async loadDepartDetail() {
      try {
        const res = await getDepartDetail(this.curId)
        console.log(res, '部门详情')
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 提交编辑后的信息ajax
    async addDepartDetail() {
      try {
        const res = await deDepartDetail(this.form)
        console.log(res, '修改提交')
        // 关闭弹层和渲染
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 确定
    hSubmit() {
    // 兜底校验,判断到底是新增还是修改
      this.$refs.deptForm.validate(valid => {
        if (!valid) return // 校验失败
        this.isEdit ? this.addDepartDetail() : this.loadDepartments()
        // 清空表单
        this.form = {}
      })
    },
    // 取消
    hCancel() {
      this.$emit('close')
    }
  }
}

</script>

<style>

</style>
